#define _CRT_SECURE_NO_WARNINGS 1
#include "StudentSubjectSelectingSystem.h"

//头节点
Node* head = NULL;

//学生信息的创建
Node* CreatInformation()
{
	Node* newnode = (Node*)malloc(sizeof(Node));
	if (newnode == NULL)
	{
		perror("malloc");
		exit(-1);
	}
	printf("请输入学生学号：\n");
	scanf("%d", &newnode->s.ID);
	printf("请输入学生姓名：\n");
	scanf("%s", newnode->s.name);
	printf("请输入课程号：\n");
	scanf("%d", &newnode->s.SubjectNum);
	printf("请输入课程名：\n");
	scanf("%s", newnode->s.SubjectName);
	printf("请输入学时：\n");
	scanf("%d", &newnode->s.Hours);
	printf("请输入学分：\n");
	scanf("%d", &newnode->s.Score);
	newnode->next = NULL;

	return newnode;
}

//学生信息的录入
void InsertInformation()
{
	//创建学生信息
	Node* newnode = CreatInformation();

	//将学生信息插入到链表中
	//尾插法
	//判断链表是否为空
	if (head == NULL)
	{
		//空链表
		head = newnode;
	}
	else
	{
		//非空链表，将学生信息尾插到链表中
		//找到尾节点
		Node* tail = head;
		while (tail->next)
		{
			tail = tail->next;
		}
		tail->next = newnode;
	}
	system("cls");
	printf("录入成功！\n");
}

//打印学生信息
void PrintInformation()
{
	//遍历链表打印
	system("cls");
	//打印表头
	printf("*********************************************\n");
	printf("*学号\t姓名\t课程号\t课程名\t学时\t学分*\n");
	printf("*********************************************\n");
	Node* cur = head;
	while (cur)
	{
		printf("%d\t%s\t%d\t%s\t%d\t%d\t\n",
			cur->s.ID,
			cur->s.name,
			cur->s.SubjectNum,
			cur->s.SubjectName,
			cur->s.Hours,
			cur->s.Score);
		cur = cur->next;
	}
}

//保存学生信息
void SaveInformation()
{
	//将学生信息保存到一个文件中
	//打开文件
	FILE* pf = fopen("studentinformation.txt", "w");
	if (pf == NULL)
	{
		perror("fopen");
		return;
	}

	char arr[100] = { 0 };
	Node* cur = head;
	//将信息写入文件中
	while (cur)
	{
		//将节点中数据转换为字符串
		sprintf(arr, "%d %s %d %s %d %d\n",
			cur->s.ID,
			cur->s.name,
			cur->s.SubjectNum,
			cur->s.SubjectName,
			cur->s.Hours,
			cur->s.Score);
		
		//将转换后的字符串写入文件中
		fputs(arr, pf);

		//遍历下一个节点
		cur = cur->next;
	}

	//关闭文件
	fclose(pf);
	pf = NULL;

	printf("数据保存成功!\n");
}

//读取学生信息
void ReadInformation()
{
	//从文件中读取数据
	//打开文件
	FILE* pf = fopen("studentinformation.txt", "r");
	if (pf == NULL)
	{
		perror("fopen");
		exit(-1);
	}

	char arr[50] = { 0 };

	//读取文件信息
	while (fgets(arr, 100, pf))//遇到文件末尾，fgets会返回NULL
	{
		Node* newnode = (Node*)malloc(sizeof(Node));
		sscanf(arr, "%d %s %d %s %d %d",
			&(newnode->s.ID),
			newnode->s.name,
			&(newnode->s.SubjectNum),
			newnode->s.SubjectName,
			&(newnode->s.Hours),
			&(newnode->s.Score));
		newnode->next = NULL;

		//尾插法
		//判断链表是否为空
		if (head == NULL)
		{
			//空链表
			head = newnode;
		}
		else
		{
			//非空链表，将学生信息尾插到链表中
			//找到尾节点
			Node* tail = head;
			while (tail->next)
			{
				tail = tail->next;
			}
			tail->next = newnode;
		}
	}

	//关闭文件
	fclose(pf);
	pf = NULL;

	printf("数据加载成功！\n");
}

//统计学生人数
void CountStudentNumber()
{
	int count = 0;
	Node* cur = head;
	while (cur)
	{
		count++;
		cur = cur->next;
	}
	printf("学生总人数为：%d\n", count);
}

//查找学生信息
void FindStudent()
{
	system("cls");
	printf("1.FindByID            2.FindByName\n");
	char ch = getch();
	switch (ch)
	{
	case '1':
	{
		printf("请输入学号：\n");
		//以学号查
		int id = 0;
		scanf("%d", &id);

		//遍历链表
		Node* cur = head;
		while (cur)
		{
			if (cur->s.ID == id)
			{
				printf("%d\t%s\t%d\t%s\t%d\t%d\t\n",
					cur->s.ID,
					cur->s.name,
					cur->s.SubjectNum,
					cur->s.SubjectName,
					cur->s.Hours,
					cur->s.Score);
				goto end1;
			}
			cur = cur->next;
		}
		//遍历完链表依旧没有找到该学生
		printf("找不到该学生！");
	}
		end1:break;
	case '2':
	{
		printf("请输入姓名：\n");
		//以姓名查
		char arr[20] = { 0 };
		scanf("%s", arr);

		Node* cur = head;
		while (cur)
		{
			if (0 == strcmp(cur->s.name, arr))
			{
				printf("%d\t%s\t%d\t%s\t%d\t%d\t\n",
					cur->s.ID,
					cur->s.name,
					cur->s.SubjectNum,
					cur->s.SubjectName,
					cur->s.Hours,
					cur->s.Score);
				goto end2;
			}
			cur = cur->next;
		}
		//遍历完该链表依旧找不到该学生
		printf("找不到该学生！");
	}
	end2:break;
	}
}

//删除学生信息
void DeleteStudnet()
{
	printf("请输入要删除的学生的姓名及学号：\n");
	int id = 0;
	char arr[20] = { 0 };
	scanf("%d %s", &id, arr);

	printf("是否要删除？\n");
	printf("1.yes   2.no\n");
	int a = 0;
	scanf("%d", &a);
	if(a == 1)
	{
		//遍历链表
		Node* cur = head;
		while (cur)
		{
			if (cur->s.ID == id || 0 == strcmp(cur->s.name, arr))//找到该学生，然后删除
			{
				//cur就是待删除的节点

				if (cur->s.ID == head->s.ID)
				{
					//要删除的节点就是头节点
					head = cur->next;
					free(cur);
					cur = NULL;
				}
				else
				{
					//要删除的节点是其他节点
					Node* prev = head;
					while (prev->next->s.ID != cur->s.ID)
					{
						prev = prev->next;
					}
					//到这里，prev就是cur的前一个节点
					prev->next = prev->next->next;
					free(cur);
					cur = NULL;
				}
				printf("删除成功！\n");
				break;
			}
			cur = cur->next;
		}
	}
	else
	{
		printf("取消删除成功！\n");
	}
}

//修改学生信息
void ReviseStudent()
{
	printf("请输入待修改的学生学号：\n");
	int id = 0;
	scanf("%d", &id);
	Node* cur = head;
	while (cur)//cur!=NULL
	{
		if (cur->s.ID == id)
		{
			//找到要修改的学生
			printf("请输入要修改的姓名：\n");
			scanf("%s", cur->s.name);

			printf("请输入要修改的课程号：\n");
			scanf("%d", &cur->s.SubjectNum);

			printf("请输入要修改的课程名：\n");
			scanf("%s", cur->s.SubjectName);

			printf("请输入要修改的学时：\n");
			scanf("%d", &cur->s.Hours);

			printf("请输入要修改的学分：\n");
			scanf("%d", &cur->s.Score);

			printf("修改成功！");
			break;
		}
		cur = cur->next;
	}
	if (cur == NULL)
	{
		printf("找不到该学生！\n");
	}
}

//销毁链表
void Destory()
{
	Node* current = head;
	while (current)
	{
		Node* next = current->next;
		free(current);
		current = next;
	}
	head = NULL;
	printf("链表已经销毁！\n");
}